/*
 * @Author: your name
 * @Date: 2021-04-16 08:57:26
 * @LastEditTime: 2021-04-19 10:55:50
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tower_defense\assets\scripts\game_scene\skills\magic_circle.ts
 */

import { LightningParams } from '../../game_data/skills/lightning_params';
import { FrameAnim } from '../../common/frame_anim';
import Enemy from '../enemy/enemy';
import { UG } from '../../modules/ugame';


const { ccclass, property } = cc._decorator;

@ccclass
export class MagicCircle extends cc.Component {

    @property(cc.Node)
    public anim_node: cc.Node = null;

    @property(cc.Graphics)
    public graphics: cc.Graphics = null;

    @property(cc.Integer)
    public lightning_effect_sample: number = 10;

    @property(cc.SpriteFrame)   // 闪电效果资源
    public lightning_effect_anim: Array<cc.SpriteFrame> = [];

    public enemy_root_node: cc.Node = null;

    public minX: number = null;
    public maxX: number = null;
    public minY: number = null;
    public maxY: number = null;

    public skill_attack: number = 10;
    public skill_speed: number = 200;
    public skill_bomb_r: number = 50;

    onLoad() {
        // 
        this.anim_node.y = 0;
        this.enemy_root_node = cc.find('Canvas/tower_panel/enemy_root');

        // 注册监听事件
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.on_touch_move_event, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.on_touch_end_event, this);

        // 计算边界
        let winsize: cc.Size = cc.winSize;
        this.minX = -winsize.width / 2 + this.node.width / 2;
        this.maxX = winsize.width / 2 - this.node.width / 2;
        this.minY = -winsize.height / 2 + this.node.height / 2;
        this.maxY = winsize.height / 2 - this.node.height / 2;

    }

    // 移动回调
    public on_touch_move_event(e): void {
        let delta: cc.Vec2 = e.getDelta();
        let nodePos: cc.Vec2 = this.node.getPosition();

        nodePos.x += delta.x;
        nodePos.y += delta.y;

        // 边界控制
        if (nodePos.x > this.maxX) {
            nodePos.x = this.maxX;
        }
        if (nodePos.x < this.minX) {
            nodePos.x = this.minX;
        }
        if (nodePos.y > this.maxY) {
            nodePos.y = this.maxY;
        }
        if (nodePos.y < this.minY) {
            nodePos.y = this.minY;
        }
        this.node.setPosition(nodePos);
    }

    // 触摸结束回调
    public on_touch_end_event(e): void {
        // 闪电参数
        this.skill_speed = LightningParams.speed;
        this.skill_attack = LightningParams.attack;
        this.skill_bomb_r = LightningParams.bomb_r;
        // 闪电动画
        this._lightning_anim();
        // 闪电伤害
        let _bomb_world_pos: cc.Vec2 = this.anim_node.parent.convertToWorldSpaceAR(cc.v2(this.anim_node.x, this.anim_node.y));
        this._on_lightning_boom(_bomb_world_pos);
        // 技能冷却
        let game_scene = cc.find('Canvas/game_scene').getComponent('game_scene');
        game_scene.lightning_is_ok = false;
    }

    // 闪电动画
    private _lightning_anim(): void {
        let frame_anim_com: FrameAnim = this.anim_node.getComponent(FrameAnim);

        // 先停止之前的动画
        frame_anim_com.stop_anim()

        // 
        frame_anim_com.sprite_frame_arr = this.lightning_effect_anim;
        frame_anim_com.sample = this.lightning_effect_sample;

        // 延迟个0.1秒，确保上一个动画已经停止
        this.scheduleOnce(() => {
            this.anim_node.y = 300;
            frame_anim_com.play_once(() => {
                // 技能动画结束
                this.node.scaleX = 0;
            });
        }, 0.1);
    }

    // 闪电伤害
    private _on_lightning_boom(_bomb_world_pos: cc.Vec2): void {
        // 世界坐标转成节点坐标
        let bomb_pos: cc.Vec2 = this.node.parent.convertToNodeSpaceAR(_bomb_world_pos);
        let bomb_r: number = this.skill_bomb_r;

        let enemy_arr: Array<cc.Node> = UG.get_enemy_arr();
        for (let i = 0; i < enemy_arr.length; i++) {
            // 判断敌人是否处于爆炸范围内
            let _enemy_world_pos: cc.Vec2 = enemy_arr[i].parent.convertToWorldSpaceAR(enemy_arr[i].getPosition());
            let enemy_pos: cc.Vec2 = this.node.parent.convertToNodeSpaceAR(_enemy_world_pos);

            let range: number = enemy_pos.sub(bomb_pos).mag();
            if (range <= bomb_r) {
                let enemy_com: Enemy = enemy_arr[i].getComponent(Enemy);
                enemy_com.on_skill_lightning_attacked(this.skill_attack);
            }
        }
    }

    onDestroy() {
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.on_touch_move_event, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.on_touch_end_event, this);
    }
}